package leetcode.code2306;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public long distinctNames(String[] ideas) {
		int arraySize = 26;
		List<String>[] starts = new ArrayList[arraySize];// 字符i的后缀字符串
		for (int i = 0; i < arraySize; i++) {
			starts[i] = new ArrayList<>();
		}
		Map<String, int[]> ends = new HashMap<>();
		int[] cnts = new int[26];// 以字符开头的字符串有多少个
		for (String idea : ideas) {
			int start = idea.charAt(0) - 'a';
			String end = idea.substring(1);
			cnts[start] += 1;
			starts[start].add(end);
			// new int[]{end后缀的词头信息，词头数量，end的后缀词头关联词};
			int[] endList = ends.getOrDefault(end, new int[] { 0, 0, 0 });
			int pos = 1 << start;
			if ((endList[0] & pos) == 0) {
				endList[0] |= pos;
				endList[1] += 1;
				ends.put(end, endList);
			}
		}
		int[] startSums = new int[26];// startSums[i] 字符i开头，所有后缀关联词数量
		for (int i = 0; i < starts.length; i++) {
			List<String> endList = starts[i];
			int startSum = 0;
			for (String end : endList) {
				startSum |= ends.get(end)[0];
			}
			int sum = 0;
			for (int j = 0; j < 26; j++) {
				int pos = 1 << j;
				if ((pos & startSum) != 0) {
					sum += cnts[j];
				}
			}
			startSums[i] = sum;
		}

		for (Map.Entry<String, int[]> entry : ends.entrySet()) {
			String end = entry.getKey();
			int[] info = entry.getValue();
			int s = info[0];
			for (int j = 0; j < 26; j++) {
				int pos = 1 << j;
//				if ((pos & end) != 0) {
//					sum += cnts[j];
//				}
			}
		}

		return 0;
	}

	@Override
	public void debug4() {
		H.compare(2, this.distinctNames(H.str2array2("[\"aaa\",\"baa\",\"caa\",\"bbb\",\"cbb\",\"dbb\"]")));

		H.compare(6, this.distinctNames(H.str2array2("[\"coffee\",\"donuts\",\"time\",\"toffee\"]")));
		// ["ra","df","dk","rk"]
		// ["pzydbfwiw","ringlxh","dcn","drmvufbt","qzydbfwiw","rrmvufbt"]
//		["pzydbfwiw","ow","ssipb","jucpsquexm","ffuzder","uftruik","ringlxh","csdbtsvg","openygbaix","dcn","sz","ejfpsea","vjfpsea","drmvufbt","qzydbfwiw","lqxik","rrmvufbt"]
		// ["pzydbfwiw","lvvyshmd","ow","ssipb","jucpsquexm","ffuzder","uftruik","ringlxh","sjlxouiv","csdbtsvg","openygbaix","dcn","sze","ttptd","atxp","ejfpsea","vjfpsea","drmvufbt","qzydbfwiw","lqxik","bjo","rrmvufbt"]
//		H.compare(2, this.distinctNames(H.str2array2("[\"ra\",\"df\",\"dk\",\"rk\"]")));
//		H.compare(2, this.distinctNames(H.str2array2("[\"ringlxh\",\"dcn\",\"drmvufbt\",\"rrmvufbt\"]")));
//		H.compare(18, this.distinctNames(H.str2array2("[\"pzydbfwiw\",\"ringlxh\",\"dcn\",\"drmvufbt\",\"qzydbfwiw\",\"rrmvufbt\"]")));
//		H.compare(438, this.distinctNames(H.str2array2("[\"pzydbfwiw\",\"lvvyshmd\",\"ow\",\"ssipb\",\"jucpsquexm\",\"ffuzder\",\"uftruik\",\"ringlxh\",\"sjlxouiv\",\"csdbtsvg\",\"openygbaix\",\"dcn\",\"sze\",\"ttptd\",\"atxp\",\"ejfpsea\",\"vjfpsea\",\"drmvufbt\",\"qzydbfwiw\",\"lqxik\",\"bjo\",\"rrmvufbt\"]")));
//		["pzydbfwiw","lvvyshmd","ow","ssipb","jucpsquexm","ffuzder","uftruik","ringlxh","jz","sjlxouiv","csdbtsvg","openygbaix","dcn","rr","hwql","ok","yy","sze","ttptd","atxp","ejfpsea","vjfpsea","lj","drmvufbt","qzydbfwiw","lqxik","bjo","rrmvufbt"]
//		H.compare(716, this.distinctNames(H.str2array2(
//				"[\"pzydbfwiw\",\"lvvyshmd\",\"ow\",\"ssipb\",\"jucpsquexm\",\"ffuzder\",\"uftruik\",\"ringlxh\",\"jz\",\"sjlxouiv\",\"csdbtsvg\",\"openygbaix\",\"dcn\",\"rr\",\"hwql\",\"ok\",\"yy\",\"sze\",\"ttptd\",\"atxp\",\"ejfpsea\",\"vjfpsea\",\"lj\",\"drmvufbt\",\"qzydbfwiw\",\"lqxik\",\"bjo\",\"rrmvufbt\"]")));
//		H.compare(4934, this.distinctNames(H.str2array2(
//				"[\"sfuzder\",\"spklurny\",\"kvdolme\",\"kbbdklkpgk\",\"za\",\"mdbsmnjiu\",\"pzydbfwiw\",\"lvvyshmd\",\"ow\",\"ssipb\",\"jucpsquexm\",\"ffuzder\",\"uftruik\",\"ringlxh\",\"jz\",\"sjlxouiv\",\"csdbtsvg\",\"openygbaix\",\"dcn\",\"r\",\"hwql\",\"ok\",\"y\",\"sze\",\"ttptd\",\"atxp\",\"ejfpsea\",\"vjfpsea\",\"lj\",\"drmvufbt\",\"zxambug\",\"ozpxq\",\"qzydbfwiw\",\"lqxik\",\"bjo\",\"rrmvufbt\",\"jvl\",\"rzxaaa\",\"nmfydhawa\",\"shlwkf\",\"rcl\",\"hhn\",\"yrmcsc\",\"yieuzizaeh\",\"nrmvufbt\",\"rinsldgdpp\",\"wqg\",\"tyhkgqcu\",\"rsdbtsvg\",\"zvehtqiqxa\",\"jtz\",\"mjaguebiy\",\"fztbpozhf\",\"zzpxq\",\"ue\",\"foktqxiqe\",\"ztf\",\"dn\",\"lxambug\",\"kinsldgdpp\",\"jhn\",\"k\",\"i\",\"qxtava\",\"roktqxiqe\",\"hr\",\"bwzw\",\"mot\",\"cadj\",\"x\",\"bcep\",\"u\",\"kzydbfwiw\",\"ahku\",\"ijo\"]")));

//		H.compare(0, this.distinctNames(H.str2array2("[\"lack\",\"back\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
